package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.TrackMapper;
import com.zretc.entity.Order;
import com.zretc.entity.Track;
import com.zretc.entity.Type;
import com.zretc.service.TrackService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@Service
public class TrackServiceImpl implements TrackService {

    @Autowired
    private TrackMapper trackMapper;


    @Override
    public PageInfo<Order> getOrderByPage(Map<String, String> param) {
        int pageNum =  Integer.parseInt(param.get("pageNum"));
        int pageSize =  Integer.parseInt(param.get("pageSize"));
        String opCode = param.get("opCode");
        String orderId = param.get("orderId");
        String typeName = param.get("typeName");
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        // 2.调用查询方法
        // 对传递给dao方法中参数进行处理
        Map<String, String> map = new HashMap<>();
        if ("".equals(opCode)||opCode==null ){
            map.put("opCode",null);
        }else {
            map.put("opCode",opCode);
        }
        if("".equals(orderId)||orderId==null){
            map.put("orderId",null);
        }else {
            map.put("orderId",orderId);
        }
        if ("".equals(typeName)||typeName==null){
            map.put("typeName",null);
        }else {
            map.put("typeName",typeName);
        }
        List<Order> orders = trackMapper.findByType(map);
        // 3.包装到PageInfo对象
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }

    /**
     * 报表
     * @param titleName
     * @param columnWidth
     * @param fileName
     * @param columnName
     * @param orders
     * @return
     */
    @Override
    public String getExcel(String titleName,
                           Integer[] columnWidth,
                           String fileName,
                           String[] columnName,
                           List<Order> orders) {
        // fileName 报表名字
        // columnWidth 列数数组
        // titleName 标题名
        // sheetName 报表文件
        // 表内的内容 dataList


        if (columnWidth.length != columnName.length) {
            return "列数和表头数不相等"; }
        // 创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(fileName);
        // 设定列数
        int columnNumber = columnWidth.length;
        // 设置每一列的宽度
            for (int i = 0; i < columnWidth.length; i++) {
            for (int j = 0; j <= i; j++) {
            if (i == j) {
                // 单独设置每列的宽
                sheet.setColumnWidth(i, columnWidth[j] * 256);
                      }
                   }
            }

            // 设置标题
            HSSFRow row1 = sheet.createRow(0);
            // 设置标题的高度
            row1.setHeightInPoints(50);
            // 创建标题单元格样式以及字体样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            // 创建字体样式
            HSSFFont headerFont1 = (HSSFFont) wb.createFont();
            // 字体加粗
            headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 设置字体类型
            headerFont1.setFontName("黑体");
            // 设置字体大小
            headerFont1.setFontHeightInPoints((short) 15);
            // 为标题样式设置字体样式
            style.setFont(headerFont1);
            // 创建第一列
            HSSFCell cell1 = row1.createCell(0);
            // 合并所有列并居中
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columnNumber - 1));
            // 设置值标题
            cell1.setCellValue(titleName);
            // 设置标题样式
            cell1.setCellStyle(style);
            // 设置表头
            HSSFRow row = sheet.createRow(1);
            // 设置表头高度
            row.setHeightInPoints(37);
            HSSFCellStyle style1 = wb.createCellStyle();

            // 设置自动换行
            style1.setWrapText(true);
            style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            // 居中格式
            style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            style1.setBottomBorderColor(HSSFColor.BLACK.index);
            style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style1.setBorderTop(HSSFCellStyle.BORDER_THIN);

            // 创建字体样式
            HSSFFont headerFont = (HSSFFont) wb.createFont();

            // 字体加粗
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            // 设置字体类型
            headerFont.setFontName("黑体");

            // 设置字体大小
            headerFont.setFontHeightInPoints((short) 10);

            // 为标题样式设置字体样式
            style1.setFont(headerFont);

            // 创建表头的列
            for (int i = 0; i < columnNumber; i++) {
            HSSFCell cell = row.createCell(i);

            cell.setCellValue(columnName[i]);

            cell.setCellStyle(style1);
            }

          int j=2;
          for(Order t:orders){
            //创建第3行
            Row rowData=sheet.createRow((short)j);
            //第一列数据
            Cell cell0=rowData.createCell((short)0);
            cell0.setCellValue(t.getOrderId());
            //cell0.setCellStyle();
            //第二列数据
            Cell cell11=rowData.createCell((short)1);
            for (Track r:t.getTracks()){
                if ("0".equals(r.getOpCode())){
                    cell11.setCellValue("揽件");
                }
                if ("1".equals(r.getOpCode())){
                    cell11.setCellValue("入库");
                }
                if ("2".equals(r.getOpCode())){
                    cell11.setCellValue("出库");
                }
                if ("3".equals(r.getOpCode())){
                    cell11.setCellValue("派送中");
                }
                      }
            Cell cell2=rowData.createCell((short)2);
            cell2.setCellValue(t.getType().getTypeName());
            Cell cell3=rowData.createCell((short)3);
            cell3.setCellValue(t.getWeight());
            j++;
               }
            // 文件夹路径
            String fileth1 = "D://EDS/ExcelReport/" ;
            //子文件夹名称
            String fileth2 =new Date().getTime()+"/";

            // 获取文件夹
            File file = new File(fileth1+fileth2+fileName+".xls");
            //获取上级文件夹
            File parentFile = file.getParentFile();
            if (!parentFile.exists() && !parentFile.isDirectory()) {
                parentFile.mkdirs();
            }
            FileOutputStream fout = null;
            try {
            fout = new FileOutputStream(file);
            wb.write(fout);

            } catch (Exception e) {
            return null;

            } finally {

            try {

            ((FileOutputStream) fout).close();

            } catch (IOException e) {

            return null ;
            }

            }
            return"输出地址" + fileth2+fileName;

    }

    @Override
    public List<Order> getByExcel(Map<String, String> param) {
        String opCode = param.get("opCode");
        String orderId = param.get("orderId");
        Map<String,String> map = new HashMap<>();
        map.put("opCode",opCode);
        map.put("orderId",orderId);
        return trackMapper.findByType(map);
    }

    /**
     * 出库
     * @param param
     * @return
     */
    @Override
    public int update(Map<String, String> param) {
        String opCode = param.get("opCode");
        String orderId = param.get("orderId");
        Map<String,String> map = new HashMap<>();
        map.put("opCode",opCode);
        map.put("orderId",orderId);
        return trackMapper.update(map);
    }

    /**
     * 添加一条物流信息记录
     *
     * @param track
     * @param: track
     * @author: bLUeSuMMerDreamer
     * @date: 2020/7/26 2:44
     * @return: java.lang.Integer
     */
    @Override
    public Integer addTrack(Track track) {
        return null;
    }


}
